package com.ljd.sevice.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.ljd.sevice.VodService;


import com.ljd.util.ConstantVodUtil;
import com.ljd.util.InitVodCilent;
import com.why.handler.GuliException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class VodServiceImpl implements VodService {

    @Override
    public String uploadAlyVideo(MultipartFile file){

        try {
            // 上传文件原始名称(**.mp4)
            String filename = file.getOriginalFilename();
            // 上传之后显示名称
            String title = filename.substring(0, filename.lastIndexOf("."));
            // 上茶UN文件输入流
            InputStream inputStream = file.getInputStream();

            UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtil.ACCESS_KEY_ID, ConstantVodUtil.ACCESS_KEY_SECRET,
                    title, filename, inputStream);

            UploadVideoImpl uploadVideo = new UploadVideoImpl();
            UploadStreamResponse response = uploadVideo.uploadStream(request);

            String videoId = null;
            videoId = response.getVideoId();
            return videoId;
        }catch (Exception e){
            e.printStackTrace();
            throw new GuliException(20001,"上传失败");
        }
    }

    @Override
    public void deleteAlyVideo(String id) {
        try {
            DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtil.ACCESS_KEY_ID, ConstantVodUtil.ACCESS_KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(id);
            DeleteVideoResponse response = client.getAcsResponse(request);
            System.out.println("云端删除视频："+response.getRequestId());
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("ErrorMessage = " + e.getLocalizedMessage());
            throw new GuliException(20001,"删除视频失败");
        }

    }

    @Override
    public void deleteBatch(List<String> videoIds) {
        try {
            DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtil.ACCESS_KEY_ID, ConstantVodUtil.ACCESS_KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            String id = StringUtils.join(videoIds.toArray(), ",");
            request.setVideoIds(id);
            DeleteVideoResponse response = client.getAcsResponse(request);
            System.out.println("云端删除视频："+response.getRequestId());
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("ErrorMessage = " + e.getLocalizedMessage());
            throw new GuliException(20001,"批量删除视频失败");
        }

    }


}
